#! /bin/sh
### BEGIN INIT INFO
# Provides:          openpanel-ssl
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ssl-proxy for openpanel
# Description:       ssl-proxy for openpanel
### END INIT INFO
#
# pound	- reverse proxy, load-balancer and https front-end for web-servers

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/pound
DESC="ssl-proxy for openpanel"
NAME=openpanel-ssl
PIDFILE=/var/run/openpanel-ssl.pid
CONFIG=/etc/openpanel/pound.cfg

# Exit if the daemon does not exist (anymore)
test -f $DAEMON || exit 0

. /lib/lsb/init-functions

rm -f /etc/openpanel/pound.pem
touch /etc/openpanel/pound.pem
chmod 600 /etc/openpanel/pound.pem
echo '# automatically generated from /etc/ssl/certs/ssl-openpanel.pem' > /etc/openpanel/pound.pem
echo '# and /etc/ssl/private/ssl-openpanel.key' >> /etc/openpanel/pound.pem
echo '' >> /etc/openpanel/pound.pem
cat /etc/ssl/certs/ssl-openpanel.pem /etc/ssl/private/ssl-openpanel.key >> /etc/openpanel/pound.pem

# The real work of an init script
case "$1" in
  start)
	log_daemon_msg "Starting $DESC" "$NAME"
	start_daemon $DAEMON -p $PIDFILE -f $CONFIG
	log_end_msg $?
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	killproc -p $PIDFILE $DAEMON
	log_end_msg $?
	;;
  restart|force-reload)
	log_daemon_msg "Restarting $DESC" "$NAME"
	killproc -p $PIDFILE $DAEMON
	start_daemon $DAEMON -p $PIDFILE -f $CONFIG
	echo "."
	;;
  status)
        pidofproc -p "$PIDFILE" >/dev/null
	status=$?
	if [ $status -eq 0 ]; then
            log_success_msg "$NAME is running"
        else
            log_success_msg "$NAME is not running"
        fi
	exit $status
        ;;
  *)
	echo "Usage: $0 {start|stop|restart|force-reload|status}"
	exit 1
	;;
esac

# Fallthrough if work done.
exit 0
